package org.finesys.ai.service.impl;

import java.util.Arrays;
import java.util.List;

import org.finesys.ai.convert.AigcMessagesConvert;
import org.finesys.ai.entity.AigcMessages;
import org.finesys.ai.mapper.AigcMessagesMapper;
import org.finesys.ai.service.AigcMessagesService;
import org.finesys.ai.vo.AigcMessageExcelVO;
import org.finesys.common.mp.core.service.impl.BaseServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Service
public class AigcMessagesSericeImpl extends BaseServiceImpl<AigcMessagesMapper, AigcMessages> implements AigcMessagesService {


    @Override
    @Cacheable(cacheNames = "aigc_messages", key = "#conversationId + '_' + #userId")
    public List<AigcMessages> getMessages(String conversationId, String userId) {
        // 查询聊天记录
        OrderItem orderItem = OrderItem.desc("create_time");
        Page<AigcMessages> page = new Page<>();
        page.setPages(1);
        page.setSize(20);
        page.setOrders(Arrays.asList(orderItem));
        return (List<AigcMessages>) this.list(page, Wrappers.<AigcMessages>lambdaQuery()
                .eq(AigcMessages::getConversationId, conversationId)
                .eq(AigcMessages::getUserId, userId).orderByDesc(AigcMessages::getCreateTime));
    }

    @Override
    @CacheEvict(cacheNames = "aigc_messages", key = "#conversationId + '_' + #userId")
    public int clearMessage(String conversionId, String userId) {
        return this.baseMapper.delete(Wrappers.<AigcMessages>lambdaQuery().eq(AigcMessages::getConversationId, conversionId).eq(AigcMessages::getUserId, userId));
    }

    
    @Override
    public List<AigcMessageExcelVO> listExcel() {
        List<AigcMessages> list = this.list();
        return AigcMessagesConvert.INSTANCE.convertList(list);
    }
}
